package net.sureon.web.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.CompanyBO;
import net.sureon.biz.CompanyMgr;
import net.sureon.biz.UserBO;
import net.sureon.biz.UserMgr;
import net.sureon.common.security.SecurityUtils;
import net.sureon.dao.Company;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 处理提交考勤 处理防止重复提交考勤 处理编辑考勤
 * 
 * @author Administrator
 * 
 */
public class RegisterInfo implements Controller {
	private static final Log logger = LogFactory.getLog(RegisterInfo.class);
	private CompanyMgr companyMgr;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		long companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();
		long userID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getUserID();

		// 获取初始公司相关信息
		CompanyBO companyBO = null; 
		companyBO = companyMgr.getCompany(companyID);
		
		String update = request.getParameter("update");
		if("true".equals(update)) {
			// 表示更新公司注册的相关信息
			String adminName = request.getParameter("adminName");
			String adminEmail = request.getParameter("adminEmail");
//			String realName = request.getParameter("realName");
//			String job = request.getParameter("job");
			String siteURL = request.getParameter("siteURL");
			String companyFullName = request.getParameter("companyFullName");
			String address = request.getParameter("address");
			String companyZipcode = request.getParameter("companyZipcode");

			String adminTitle = request.getParameter("adminTitle");
			String companyWebSite = request.getParameter("companyWebSite");
			
			String adminTel1 = request.getParameter("adminTel1");
			String adminTel2 = request.getParameter("adminTel2");
			String adminTel = adminTel1 + "-" + adminTel2;
			
			String adminFax1 = request.getParameter("adminFax1");
			String adminFax2 = request.getParameter("adminFax2");
			String adminFax = adminFax1 + "-" + adminFax2;
			
			Company companyVO = companyBO.getCompanyVO();
			companyVO.setAdminName(adminName);
			companyVO.setAdminEmail(adminEmail);
			companyVO.setSiteURL(siteURL);
			companyVO.setCompanyFullName(companyFullName);
			companyVO.setCompanyWebSite(companyWebSite);
			companyVO.setAdminTitle(adminTitle);
			
//				companyVO.setAdminAddress(address);
			companyVO.setCompanyAddress(address);
			companyVO.setCompanyZipCode(companyZipcode);
			companyVO.setAdminTel(adminTel);
			companyVO.setAdminFax(adminFax);
			
		
			int updateCompanyRows = companyMgr.updateCompany(companyBO);
			if(updateCompanyRows <= 0) {
				logger.error("公司信息更新失败！");
			}
		}
		
		if(companyBO != null) {
			request.setAttribute("companyBO", companyBO);
		}

		return new ModelAndView("admin/registerInfo");
	}


	public CompanyMgr getCompanyMgr() {
		return companyMgr;
	}

	public void setCompanyMgr(CompanyMgr companyMgr) {
		this.companyMgr = companyMgr;
	}
}
